package net.sahv.bdyz.api;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

import com.alibaba.fastjson.JSON;

import net.sahv.bdyz.model.Admin;
import net.sahv.bdyz.model.Domain;
import net.sahv.bdyz.model.Member;
import net.sahv.bdyz.service.AdminService;
import net.sahv.bdyz.service.DomainService;
import net.sahv.bdyz.service.MemberService;

/**
 * 
 * @author 任凯伟
 *
 */
@Controller("apiMemberController")
@RequestMapping("api/member")
public class MemberController {
	@Autowired
	private MemberService memberService;// 会员
	@Autowired
	private AdminService adminService;
	@Autowired
	CookieLocaleResolver resolver;
	@Autowired
	private DomainService domainService;// 地区
	
	/**
	 * 
	 * @author 作者 : 任凯伟
	 * @date 创建时间：2016年8月17日 下午2:13:03
	 * @version 1.0
	 * @see 判断用户是否登录
	 * @param
	 * @parameter
	 * @since
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/isLogin")
	public boolean isLogin() {
		Member member = memberService.getCurrent();
		if (member != null) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 * @author 作者 : 任凯伟
	 * @date 创建时间：2016年9月8日 上午11:23:36
	 * @version 1.0
	 * @see
	 * @param
	 * @parameter
	 * @since
	 * @return
	 */
	@RequestMapping(value = "/userCenter", method = RequestMethod.GET)
	public String userCenter(Model model) {
		Member member = memberService.getCurrent();
		model.addAttribute("member", member);
		model.addAttribute("flag","5");
		return "phone/userCenter/userCenter";
	}

	/**
	 * 
	 * @author 作者 : 任凯伟
	 * @date 创建时间：2016年9月8日 上午11:43:58
	 * @version 1.0
	 * @see 退出
	 * @param
	 * @parameter
	 * @since
	 * @return
	 */
	@RequestMapping(value = "/exit")
	public String login(String redirectUrl, HttpServletResponse response, HttpServletRequest request,
			Model model) {
		HttpSession session = request.getSession();
		session.setMaxInactiveInterval(1);
		resolver.setLocale(request, response, Locale.CHINA);
		model.addAttribute("redirectUrl", redirectUrl);
		return "phone/login/login";
	}

	/**
	 * 
	 * @author 作者 : 任凯伟
	 * @date 创建时间：2016年9月8日 下午2:13:21
	 * @version 1.0
	 * @see 跳转修改密码页面
	 * @param
	 * @parameter
	 * @since
	 * @return
	 */
	@RequestMapping(value = "/modifyPassword", method = RequestMethod.GET)
	public String modifyPassword(Model model) {
		return "phone/login/modifyPassword";
	}

	/**
	 * 
	 * @author 作者 : 任凯伟
	 * @date 创建时间：2016年9月9日 上午9:57:46
	 * @version 1.0
	 * @see 跳转到我的业务员
	 * @param
	 * @parameter
	 * @since
	 * @return
	 */
	@RequestMapping(value = "/mySalesman", method = RequestMethod.GET)
	public String mySalesman(Model model) {
		Member member = memberService.getCurrent();
		if (member == null) {
			return "phone/main/index";
		} else {
			if (member.getmMessage1() != null) {
				Admin admin = adminService.selectByPrimaryKey(Integer.valueOf(member.getmMessage1()));
				model.addAttribute("admin", admin);
			}
		}
		return "phone/userCenter/mySalesman";
	}

	/**
	 * 
	 * @author 作者 : 任凯伟
	 * @date 创建时间：2016年9月22日 上午10:14:52
	 * @version 1.0
	 * @see
	 * @param
	 * @parameter
	 * @since
	 * @return
	 */
	@RequestMapping(value = "/userInfomation", method = RequestMethod.GET)
	public String userInfomation(Model model) {
		Member member = memberService.getCurrent();
		if (member == null) {
			return "phone/main/index";
		} else {
			// 省
			if (member.getmProvince() != null) {
				Domain pDomain = domainService.findByCode(member.getmProvince());
				if (pDomain != null) {
					model.addAttribute("mProvince", member.getmProvince());
					member.setmProvince(pDomain.getdName());
				} else {
					member.setmProvince("");
				}
			}
			// 市
			if (member.getmCity() != null) {
				Domain cDomain = domainService.findByCode(member.getmCity());
				if (cDomain != null) {
					model.addAttribute("mCity", member.getmCity());
					member.setmCity(cDomain.getdName());
				} else {
					member.setmCity("");
				}
			}
			// 县
			if (member.getmArea() != null) {
				Domain aDomain = domainService.findByCode(member.getmArea());
				if (aDomain != null) {
					model.addAttribute("mArea", member.getmArea());
					member.setmArea(aDomain.getdName());
				} else {
					member.setmArea("");
				}
			}
			model.addAttribute("member", member);
		}
		List<Domain> domainList = domainService.selectAll();
		model.addAttribute("domainList", domainList);
		return "phone/userCenter/userInfomation";
	}

	/**
	 * 
	 * @author 作者 : 任凯伟
	 * @date 创建时间：2016年10月7日 下午2:32:07
	 * @version 1.0
	 * @see
	 * @param
	 * @parameter
	 * @since
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ApiResult update(Member member) {
		ApiResult result = new ApiResult();
		Member currentMem = memberService.getCurrent();
		if (member == null) {
			result.msg.status = 0;
			return result;
		} else {
			/*currentMem.setmAddress(member.getmAddress());*/
			currentMem.setmProvince(member.getmProvince());
			currentMem.setmCity(member.getmCity());
			currentMem.setmArea(member.getmArea());
			currentMem.setmEmail(member.getmEmail());
			currentMem.setmFixedTelephone(member.getmFixedTelephone());
			currentMem.setmImg(member.getmImg());
			currentMem.setmNickname(member.getmNickname());
			int count = memberService.updateByPrimaryKey(currentMem);
			if (count > 0) {
				result.msg.status = 1;
			} else {
				result.msg.status = 0;
			}
		}
		return result;
	}

	/**
	 * 
	 * @author 作者 : 任凯伟
	 * @date 创建时间：2016年10月7日 下午1:56:35
	 * @version 1.0
	 * @see 获取子级节点
	 * @param
	 * @parameter
	 * @since
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getChildArea", method = RequestMethod.POST)
	private JSON getCity(String pId) {
		List<Domain> areaList = domainService.selectByParentId(pId);
		return (JSON) JSON.toJSON(areaList);
	}
	
}
